﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace orderCreater
{
    public partial class FrmWdhUser : Form
    {
        public OrderCreaterModel OCM = new OrderCreaterModel(ConfigurationManager.AppSettings["ConnStr-wdh"]);
        public FrmWdhUser()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            this.sr_bgw.WorkerSupportsCancellation = true;
        }

        private void sr_btn_stop_Click(object sender, EventArgs e)
        {
            this.sr_lb_status.Text = "停止中……";
            sr_bgw.CancelAsync();
        }

        private void sr_btn_start_Click(object sender, EventArgs e)
        {
            if (this.tb_total_count.Text.Length < 1)
            {
                MessageBox.Show("请输入总条数");
            }
            else
            {
                this.sr_lb_status.Text = "系统初始化中……";
                sr_btn_start.Enabled = false;
                sr_btn_stop.Enabled = true;
                this.sr_bgw.RunWorkerAsync();
            }
        }

        private void tb_total_count_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键  
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字  
                {
                    e.Handled = true;
                }
            }
        }

        private void sr_bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = (BackgroundWorker)sender;

            int total = 0;
            int post_err_cnt = 0;
            StringBuilder _logs = new StringBuilder();
            do
            {
                if (worker.CancellationPending || total >= int.Parse(this.tb_total_count.Text.ToString()))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    ///获取新增用户
                    WDH_REGISTER new_user = OCM.WDHNewUser();
                    if (new_user.status == 200)
                    {
                        total++; 
                        _logs.Insert(0, "[" + DateTime.Now + "]生成用户信息成功! 用户ID:" + new_user.data.member_id + "，用户名：" + new_user.data.member_name + "，真实姓名：" + new_user.data.member_truename + "。\r\n");
                        this.sr_tb_logs.Text = _logs.ToString();
                        this.sr_lb_count.Text = total.ToString();
                        this.sr_lb_status.Text = "运行中……";
                    }
                    else
                    {
                        _logs.Insert(0, "[" + DateTime.Now + "]生成用户信息失败：" + new_user.msg + "。\r\n");
                        this.sr_tb_logs.Text = _logs.ToString();
                        post_err_cnt++;
                        if (post_err_cnt > 10)
                        {
                            this.sr_lb_status.Text = "停止中……";
                            sr_bgw.CancelAsync();
                        }
                    }
                    Random rd_sl = new Random();
                    int t_sl = rd_sl.Next(20 * 1000, Int32.Parse(this.tb_jiange.Text) * 1000);
                    System.Threading.Thread.Sleep(t_sl);
                }
            } while (!worker.CancellationPending);
        }

        private void sr_bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            sr_btn_stop.Enabled = false;
            sr_btn_start.Enabled = true;
            this.sr_lb_status.Text = "已停止！";
        }
    }
}
